﻿using System;
using System.Security.Cryptography;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Security;
namespace FTchina.Library
{
    public class SecurityHelper
    {
        /// <summary>
        /// 加密MD5
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string EncryptMD5(string str)
        {
            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5");
        }
        /// <summary>
        /// 加密SHA1
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string EncryptSHA1(string str)
        {
            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "SHA1");
        }
        /// <summary>
        /// 内部数据加解密密钥
        /// </summary>
        public const string CONST_CRYPT_KEY = "FTchinaCMSMSSqlSMFTchinaCMSMSSql";
        /// <summary>
        /// 使用预设密钥加密
        /// </summary>
        /// <param name="_source"></param>
        /// <returns></returns>
        public static string Encrypt(string _source)
        {
            return Encrypt(_source, CONST_CRYPT_KEY);
        }
        /// <summary>
        /// 使用自定义密钥加密
        /// </summary>
        /// <param name="_source"></param>
        /// <param name="_key"></param>
        /// <returns></returns>
        public static string Encrypt(string _source, string _key)
        {
            string strSource, strKey;
            strKey = _key.Substring(0, 8);
            Byte[] byKey = ASCIIEncoding.ASCII.GetBytes(strKey);
            strKey = _key.Substring(8, 8);
            Byte[] byIV = ASCIIEncoding.ASCII.GetBytes(strKey);
            DES objDES = new DESCryptoServiceProvider();
            objDES.Key = byKey;
            objDES.IV = byIV;
            byte[] byInput = Encoding.Default.GetBytes(_source);
            MemoryStream objMemoryStream = new MemoryStream();
            CryptoStream objCryptoStream = new CryptoStream(objMemoryStream, objDES.CreateEncryptor(), CryptoStreamMode.Write);
            objCryptoStream.Write(byInput, 0, byInput.Length);
            objCryptoStream.FlushFinalBlock();
            StringBuilder objStringBuilder = new StringBuilder();
            foreach (byte b in objMemoryStream.ToArray())
            {
                objStringBuilder.AppendFormat("{0:X2}", b);
            }
            strSource = objStringBuilder.ToString();
            objCryptoStream.Close();
            objMemoryStream.Close();
            return strSource.ToString();
        }
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="_source"></param>
        /// <returns></returns>
        public static string Decrypt(string _source)
        {
            return Decrypt(_source, CONST_CRYPT_KEY);
        }
        /// <summary>
        /// 自定义密钥解密
        /// </summary>
        /// <param name="_source"></param>
        /// <param name="_key"></param>
        /// <returns></returns>
        public static string Decrypt(string _source, string _key)
        {
            string strSource, strKey;
            try
            {
                strKey = _key.Substring(0, 8);
                Byte[] bytKey = ASCIIEncoding.ASCII.GetBytes(strKey);
                strKey = _key.Substring(8, 8);
                Byte[] bytIV = ASCIIEncoding.ASCII.GetBytes(strKey);
                DES objDES = new DESCryptoServiceProvider();
                objDES.Key = bytKey;
                objDES.IV = bytIV;
                byte[] bytInputByteArray = new byte[_source.Length / 2];
                for (int x = 0; x < (_source.Length / 2); x++)
                {
                    int i = (Convert.ToInt32(_source.Substring(x * 2, 2), 16));
                    bytInputByteArray[x] = (byte)i;
                }
                MemoryStream objMemoryStream = new MemoryStream();
                CryptoStream objCryptoStream = new CryptoStream(objMemoryStream, objDES.CreateDecryptor(), CryptoStreamMode.Write);
                objCryptoStream.Write(bytInputByteArray, 0, bytInputByteArray.Length);
                objCryptoStream.FlushFinalBlock();
                strSource = Encoding.Default.GetString(objMemoryStream.ToArray());
                objMemoryStream.Close();
            }
            catch
            {
                strSource = "";
            }
            return strSource;
        }
    }
}
